<template>
  <div class="saleCondition">
    <div>
      <el-table
        v-if="flag"
        :data="tableObj.temporary"
        stripe
        style="width: 100%"
        @row-click="handleRow"
      >
        <el-table-column prop="index" label="序号" width="100">
        </el-table-column>

        <el-table-column prop="imgPath" label="图片" width="120">
          <template slot-scope="scope">
            <img
              :src="require('/src/assets/img/' + scope.row.imgPath)"
              width="50"
              height="50"
            />
          </template>
        </el-table-column>

        <el-table-column prop="name" label="产品名称" width="100">
        </el-table-column>

        <el-table-column prop="number" label="数量" width="100">
        </el-table-column>

        <el-table-column prop="money" label="金额" width="100">
        </el-table-column>

        <el-table-column prop="buyTime" label="下单时间" width="160">
        </el-table-column>

        <el-table-column prop="address" label="收货地址" width="180">
        </el-table-column>

        <el-table-column prop="receiver" label="收货人" width="100">
        </el-table-column>

        <el-table-column prop="phone" label="收货人电话" width="120">
        </el-table-column>

        <el-table-column
          prop="username"
          label="下单用户"
          width="150"
        ></el-table-column>

        <el-table-column prop="" label="订单状态" width="150">
          <template slot-scope="scope">
            <el-button
              type="success"
              icon="el-icon-circle-check"
              v-if="scope.row.status == 0"
              @click="sendGoods"
              >发货</el-button
            >
            <div
              v-if="scope.row.status == 1"
              style="
                color: #ddd;
                border: none;
                width: 100px;
                height: 30px;
                line-height: 30px;
                text-align: center;
              "
            >
              已发货
            </div>
          </template>
        </el-table-column>
      </el-table>
      <Pages :tableObjs="tableObj" v-if="flag"></Pages>
    </div>
  </div>
</template>

<script>
// import { requestResult } from "../../util/request";
import { getRequest } from "../../util/tools";
import { postRequest, getReq } from "../../util/request";
import Pages from "../Pages.vue";
export default {
  name: "MySaleCondition",
  components: { Pages },
  data() {
    return {
      flag: false,
      tableObj: {
        tableData: [],
        number: 3,
        totalPages: 0,
        currentPage: 1,
        temporary: [],
      },
      userId: "",
      rowData: "",
    };
  },
  methods: {
    //获取请求结果
    async result() {
      await getRequest(
        `http://localhost:3000/user/getSaleCondition?userId=${this.userId}`,
        this.tableObj
      );
      this.flag = true;
      this.tableObj.number =
        this.tableObj.tableData.length >= 6 ? 6 : this.tableObj.number;
    },
    //发货按钮
    sendGoods() {
      setTimeout(() => {
        // console.log("this.rowData", this.rowData);
        if (window.confirm("确定发货吗？")) {
          //获取茶叶id
          let getTeaId = getReq("order/getTeaId", `?id=${this.rowData.id}`);
          getTeaId
            .then(
              (res) => {
                return res.data[0].teaId;
              },
              (err) => {
                throw err;
              }
            )
            .then((value) => {
              console.log(value, this.rowData.id);
              //发货后减少商家的库存
              let declineResult = postRequest("user/declineTeaStore", {
                store: this.rowData.number,
                teaId: value,
              });
              declineResult.then(
                (res) => {
                  //先判断库存是否足够发货
                  if (res.data.ok) {
                    //修改订单发货状态
                    let modifyStatus = postRequest("order/modifyStatus", {
                      id: this.rowData.id,
                    });
                    modifyStatus
                      .then(
                        (res) => {
                          this.$message({
                            type: "success",
                            message: res.data.msg,
                            duration: 3000,
                          });
                          this.result();
                        },
                        (err) => {
                          throw err;
                        }
                      )
                      .then(() => {
                        //修改商家金额
                        let modifyMoney = postRequest("user/modifyMoney", {
                          id: this.userId,
                          money: this.rowData.money,
                        });
                        modifyMoney.then((res) => {
                          this.$message({
                            type: "success",
                            message: res.data.msg,
                            duration: 3000,
                          });
                          this.$bus.$emit("modify", true);
                        });
                      });
                  } else {
                    this.$message({
                      type: "warning",
                      message: res.data.msg,
                      duration: 3000,
                    });
                  }
                },
                (err) => {
                  throw err;
                }
              );
            });
        }
      });
    },
    //点击行
    handleRow(row) {
      this.rowData = row;
    },
  },
  mounted() {
    this.userId = this.$cookie.get("userId");
    // this.getRequestResult();
    this.result();
    // console.log(this.tableObj);
  },
};
</script>

<style scoped lang='less'>
.saleCondition {
  width: 100%;
  min-height: 300px;
  margin: 20px 10px;
  // display: flex;
  // justify-content: center;
}
</style>